/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.ReceiveBillDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.ReceiveBill;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class ReceiveBillDaoImpl extends BaseDaoImpl implements ReceiveBillDao {
	public ReceiveBill get(String billCode) {
		String sql = "from com.minierp.model.ReceiveBill as obj where obj.billCode = :billCode and obj.enabled=\'Y\' order by obj.id";
		String[] paramNames = new String[]{"billCode"};
		Object[] values = new Object[]{billCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (ReceiveBill) list.get(0);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.ReceiveBill where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getList(String searchKey, boolean examined, long tsId,
			UserBean ub, long estManagerId, Date allowStart, int startIndex,
			int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		String examineFlag = "N";
		String orderByStr = "order by b.receiveDate";
		if (examined) {
			examineFlag = "Y";
			orderByStr = "order by b.receiveDate desc,b.billCode desc";
		}

		String sql = "select b,e from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str
				+ " and b.examined=:examined"
				+ " and b.enabled=\'Y\' "
				+ orderByStr;
		Session sess = null;

		List arg17;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			q.setString("examined", examineFlag);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg17 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg17;
	}

	public int getListCount(String searchKey, boolean examined, long tsId,
			UserBean ub, long estManagerId, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.examined=:examined" + " and b.enabled=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			q.setString("examined", examineFlag);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg15 = Integer.parseInt(list.get(0).toString());
				return arg15;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public BigDecimal getListCost(String searchKey, boolean examined,
			long tsId, UserBean ub, long estManagerId, Date allowStart)
			throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select sum(b.cost) from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.examined=:examined" + " and b.enabled=\'Y\'";
		Session sess = null;

		BigDecimal arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			q.setString("examined", examineFlag);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				arg15 = new BigDecimal(list.get(0).toString());
				return arg15;
			}

			arg15 = new BigDecimal(0);
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public List<?> getList(String searchKey, long tsId, UserBean ub,
			String orderBy, long estManagerId, Date allowStart, int startIndex,
			int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		String sql = "select b,e from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.enabled=\'Y\'";
		if (orderBy != null && orderBy.equals("RECEIVE_DATE_ASC")) {
			sql = sql + " order by b.receiveDate asc,b.billCode asc";
		} else {
			sql = sql + " order by b.receiveDate desc,b.billCode desc";
		}

		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public int getListCount(String searchKey, long tsId, UserBean ub,
			long estManagerId, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		String sql = "select count(b.id) from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.enabled=\'Y\'";
		Session sess = null;

		int arg13;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (list.isEmpty()) {
				return 0;
			}

			arg13 = Integer.parseInt(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg13;
	}

	public List<?> getList(long clientId, boolean examined, UserBean ub) {
		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "from com.minierp.model.ReceiveBill b where b.clientId="
				+ clientId
				+ " and b.examined=\'"
				+ examineFlag
				+ "\'"
				+ " and b.enabled=\'Y\'"
				+ EncCommonHelper
						.createTallySetSQLStr("b.tallySetId", ub, true)
				+ " order by b.receiveDate desc,b.billCode desc";
		return this.getHibernateTemplate().find(sql);
	}

	public BigDecimal getCost(long businessBillId, boolean examined) {
		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select sum(i.thisCost)  from com.minierp.model.ReceiveBill b ,com.minierp.model.ReceiveBillItem i where b.id=i.billId and i.businessBillId="
				+ businessBillId
				+ " and b.examined=\'"
				+ examineFlag
				+ "\'"
				+ " and b.enabled=\'Y\'";
		List list = this.getHibernateTemplate().find(sql);
		return !list.isEmpty() && list.get(0) != null ? new BigDecimal(list
				.get(0).toString()) : new BigDecimal(0);
	}

	public Object[] getStatusValues(long id) {
		String sql = "select b.updateTime,b.enabled,b.examined from com.minierp.model.ReceiveBill b where b.id="
				+ id;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 ? (Object[]) list.get(0) : null;
	}

	public BigDecimal getSmsCost(Date startDate, Date endDate) throws Exception {
		String sql = "select sum(b.cost) from com.minierp.model.ReceiveBill b where b.enabled=\'Y\' and b.examined=\'Y\' and b.receiveDate>=:startDate and b.receiveDate<:endDate";
		Session sess = null;

		BigDecimal arg7;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				arg7 = new BigDecimal(list.get(0).toString());
				return arg7;
			}

			arg7 = new BigDecimal(0);
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg7;
	}

	public int estCorporate(final long fromClientId, final long toClientId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.ReceiveBill set clientId="
								+ toClientId
								+ " ,updateTime=:updateTime"
								+ " where clientId=" + fromClientId;
						Query q = session.createQuery(hql);
						q.setTimestamp("updateTime", new Date());
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getListByBusinessBill(long billId) {
		String sql = "select b from com.minierp.model.ReceiveBill b ,com.minierp.model.ReceiveBillItem s where b.id=s.billId and s.businessBillId="
				+ billId + " and b.enabled=\'Y\'" + " order by b.receiveDate";
		return this.getHibernateTemplate().find(sql);
	}

	public BigDecimal getBusinessDateCount(Date startDate, Date endDate,
			long tsId, UserBean ub) throws Exception {
		String str = "";
		if (tsId != 0L) {
			str = str + " and b.tally_set_id=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tally_set_id",
							ub, true);
		}

		String sql = "select sum(cost) from receive_bill b where b.examined=\'Y\' and b.enabled=\'Y\' and b.receive_date>=:startDate and b.receive_date<:endDate"
				+ str;
		Session sess = null;

		BigDecimal arg11;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			List list = q.list();
			if (list == null || list.isEmpty() || list.get(0) == null) {
				arg11 = new BigDecimal(0);
				return arg11;
			}

			arg11 = new BigDecimal(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg11;
	}

	public int getBusinessDateCountNum(Date startDate, Date endDate, long tsId,
			UserBean ub) throws Exception {
		String str = "";
		if (tsId != 0L) {
			str = str + " and b.tally_set_id=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tally_set_id",
							ub, true);
		}

		String sql = " select count(i.id) from receive_bill b,receive_bill_item i where b.id=i.bill_id and b.examined=\'Y\' and b.enabled=\'Y\' and b.receive_date>=:startDate and b.receive_date<:endDate"
				+ str;
		Session sess = null;

		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			List list = q.list();
			if (list != null && !list.isEmpty() && list.get(0) != null) {
				int arg11 = Integer.parseInt(list.get(0).toString());
				return arg11;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List getExamSmsCount(long[] tsIdArr) {
		if (tsIdArr != null && tsIdArr.length != 0) {
			String sql = "select b.billCode from  com.minierp.model.ReceiveBill b where b.examSms=\'Y\'"
					+ PageHelper.createIdArrStr(tsIdArr, "b.tallySetId", true);
			return this.getHibernateTemplate().find(sql);
		} else {
			return new ArrayList();
		}
	}

	public List getExamSmsCount() {
		String sql = "select b.billCode from  com.minierp.model.ReceiveBill b where b.examSms=\'Y\'";
		return this.getHibernateTemplate().find(sql);
	}

	public int updateExamSms() {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.ReceiveBill set examSms=\'N\' ,updateTime=:updateTime where examSms=\'Y\'";
						Query q = session.createQuery(hql);
						q.setTimestamp("updateTime", new Date());
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List getBaCost(long tsId, UserBean ub, Date startDate, Date endDate)
			throws Exception {
		String str = "";
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select sum(b.cost),b.bankAccountId from com.minierp.model.ReceiveBill b where b.enabled=\'Y\' and b.examined=\'Y\' and b.receiveDate>=:startDate and b.receiveDate<:endDate"
				+ str + " group by b.bankAccountId";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	private String getAppBcListSql(Date startDate, Date endDate,
			String estName, String type, long tsId, UserBean ub, long mngId) {
		String recStr = "";
		String busStr = "";
		String str = "";
		if (startDate != null) {
			recStr = recStr + " and b.receive_date>=:startDate";
			busStr = busStr + " and b.order_date>=:startDate";
		}

		if (endDate != null) {
			recStr = recStr + " and b.receive_date<:endDate";
			busStr = busStr + " and b.order_date<:endDate";
		}

		if (estName != null && !estName.equals("")) {
			str = str + " and (e.est_name like :estName)";
		}

		if (mngId != 0L) {
			str = str + " and e.manager_id=" + mngId;
		}

		if (tsId != 0L) {
			str = str + " and b.tally_set_id=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tally_set_id",
							ub, true);
		}

		String recSql = " select b.id,e.est_name,null as linkman,\'RECE\' as type,b.cost,b.bank_account_id,b.receive_date as bill_date,b.creater_name,b.create_time from receive_bill b,establishment e where b.client_id=e.id and b.examined=\'Y\' and b.enabled=\'Y\'"
				+ recStr + str;
		String busSql = " select b.id,e.est_name,b.linkman,\'BUSI\' as type,b.imprest as cost,b.bank_account_id,b.order_date as bill_date,b.creater_name,b.create_time from business_bill b,business_bill_ext be,establishment e where b.id=be.id and b.client_id=e.id and be.examined=\'Y\' and be.enabled=\'Y\' and b.imprest!=0"
				+ busStr + str;
		String sql = "";
		if (type != null && !type.equals("")) {
			if (type.equals("RECE")) {
				sql = recSql;
			} else {
				sql = busSql;
			}
		} else {
			sql = recSql + " union all " + busSql;
		}

		return sql;
	}

	private void setAppBcListSqlVal(Query q, Date startDate, Date endDate,
			String estName) {
		if (startDate != null) {
			q.setDate("startDate", startDate);
		}

		if (endDate != null) {
			q.setDate("endDate", endDate);
		}

		if (estName != null && !estName.equals("")) {
			q.setString("estName", "%" + estName + "%");
		}

	}

	public List getAppBcListCount(Date startDate, Date endDate, String estName,
			String type, long tsId, UserBean ub, long mngId) throws Exception {
		String sql = this.getAppBcListSql(startDate, endDate, estName, type,
				tsId, ub, mngId);
		sql = " select count(*),sum(cost) from (" + sql + ") a";
		Session sess = null;

		List arg13;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			this.setAppBcListSqlVal(q, startDate, endDate, estName);
			arg13 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg13;
	}

	public List getAppBcList(Date startDate, Date endDate, String estName,
			String type, long tsId, UserBean ub, long mngId, int startIndex,
			int num) throws Exception {
		String sql = this.getAppBcListSql(startDate, endDate, estName, type,
				tsId, ub, mngId);
		sql = " select * from (" + sql + ") a order by bill_date desc";
		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			this.setAppBcListSqlVal(q, startDate, endDate, estName);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public List getAppListForExamCount(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId) throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*),sum(b.cost) from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.examined=\'N\'" + " and b.enabled=\'Y\'";
		Session sess = null;

		List arg12;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			arg12 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg12;
	}

	public List getAppListForExam(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId, int startIndex, int num)
			throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select b,e.estName from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str
				+ " and b.examined=\'N\'"
				+ " and b.enabled=\'Y\'"
				+ " order by b.receiveDate desc,b.billCode desc";
		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public List getAppListForUnExamCount(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId) throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*),sum(b.cost) from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.examined=\'Y\'" + " and b.enabled=\'Y\'";
		Session sess = null;

		List arg12;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			arg12 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg12;
	}

	public List getAppListForUnExam(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId, int startIndex, int num)
			throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.receiveDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select b,e.estName from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str
				+ " and b.examined=\'Y\'"
				+ " and b.enabled=\'Y\'"
				+ " order by b.examineTime desc";
		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public List getAppBusiDateCoun(Date startDate, Date endDate, long tsId,
			UserBean ub, long mngId) throws Exception {
		String sql = " select count(*),sum(i.this_cost) from receive_bill b,receive_bill_item i,establishment e where b.id=i.bill_id and b.client_id=e.id and b.examined=\'Y\' and b.enabled=\'Y\'";
		if (startDate != null) {
			sql = sql + " and b.receive_date>=:startDate";
		}

		if (endDate != null) {
			sql = sql + " and b.receive_date<:endDate";
		}

		if (mngId != 0L) {
			sql = sql + " and e.manager_id=" + mngId;
		}

		if (tsId != 0L) {
			sql = sql + " and b.tally_set_id=" + tsId;
		} else {
			sql = sql
					+ EncCommonHelper.createTallySetSQLStr("b.tally_set_id",
							ub, true);
		}

		Session sess = null;

		List arg11;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			arg11 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg11;
	}

	public List getAppListCount(Date startDate, Date endDate, String estName,
			long baId, long tsId, UserBean ub, long mngId) throws Exception {
		String str = "";
		if (startDate != null) {
			str = str + " and b.receiveDate>=:startDate";
		}

		if (endDate != null) {
			str = str + " and b.receiveDate<:endDate";
		}

		if (estName != null && !estName.equals("")) {
			str = str + " and (e.estName like :estName)";
		}

		if (baId != 0L) {
			str = str + " and b.bankAccountId=" + baId;
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*),sum(b.cost) from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.enabled=\'Y\'";
		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			if (estName != null && !estName.equals("")) {
				q.setString("estName", "%" + estName + "%");
			}

			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public List getAppList(Date startDate, Date endDate, String estName,
			long baId, long tsId, UserBean ub, long mngId, String orde,
			int startIndex, int num) throws Exception {
		String str = "";
		if (startDate != null) {
			str = str + " and b.receiveDate>=:startDate";
		}

		if (endDate != null) {
			str = str + " and b.receiveDate<:endDate";
		}

		if (estName != null && !estName.equals("")) {
			str = str + " and (e.estName like :estName)";
		}

		if (baId != 0L) {
			str = str + " and b.bankAccountId=" + baId;
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String ordeStr = " order by b.receiveDate desc,b.billCode desc";
		if (orde != null) {
			if (orde.equals("RECE_DATE")) {
				ordeStr = " order by b.receiveDate desc,b.billCode desc";
			} else if (orde.equals("RECE_COST")) {
				ordeStr = " order by b.cost desc,b.billCode desc";
			} else if (orde.equals("BILL_CODE")) {
				ordeStr = " order by b.billCode desc";
			} else if (orde.equals("ESTA_NAME")) {
				ordeStr = " order by e.nameSpell desc,b.billCode desc";
			}
		}

		String sql = "select b,e.estName from com.minierp.model.ReceiveBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.enabled=\'Y\'" + ordeStr;
		Session sess = null;

		List arg19;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			if (estName != null && !estName.equals("")) {
				q.setString("estName", "%" + estName + "%");
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg19 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg19;
	}
}